package pr2.mutacion;

import java.util.ArrayList;
import java.util.List;

import pr2.cromosoma.CromosomaPVC;

public class MutacionInversion implements EstrategiaMutacion<CromosomaPVC> {

	@Override
	public void muta(CromosomaPVC cromosoma, double prob) {

		double probabilidad = Math.random();
		Integer pos1, pos2, posAux;
		List<Integer> aux;

		if (probabilidad <= prob) {

			pos1 = new Double(Math.random() * (cromosoma.getFenotipo().size())).intValue();
			pos2 = new Double(Math.random() * (cromosoma.getFenotipo().size())).intValue();

			if (!pos1.equals(pos2)) {

				if (pos2 < pos1) {
					posAux = pos1;
					pos1 = pos2;
					pos2 = posAux;
				}

				aux = new ArrayList<Integer>();

				for (int i = pos1; i <= pos2; i++) {
					aux.add(cromosoma.getFenotipo().get(i));
				}

				for (int i = aux.size() - 1; i >= 0; i--) {
					cromosoma.getFenotipo().set(pos2 - i, aux.get(i));
				}

			}

		}

	}

}
